﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KL.Store.Core.Web;
using KL.Store.Web.Models;
using KL.Store.Data;
using KL.Store.Core.Domain;
using KL.Store.Core.Domain.Enum;
using KL.Store.Core;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace KL.Store.Web.Controllers
{
    [Authorize]
    public class VoucherController : KL.Store.Core.Web.BaseController
    {

        public ActionResult Index()
        {
            return View();
        }

        [Child]
        public ActionResult Get(VoucherListModel tmpModel, string button)
        {
            VoucherListModel vModel = new VoucherListModel();
            if (eStoreUser.AccountId > 0)
            {
                vModel.Vouchers = dbVoucher.GetList(eStoreUser.AccountId);
                if (vModel.Vouchers == null) vModel.Vouchers = new List<Voucher>();
                foreach (Voucher voucherTemp in tmpModel.Vouchers)
                {
                    if (!voucherTemp.Selected)
                    {
                        Voucher voucher = vModel.Vouchers.Where(v => v.VoucherId == voucherTemp.VoucherId).FirstOrDefault();
                        if (voucher != null)
                        {
                            voucher.Selected = false;
                        }
                    }
                }
                vModel.Email = string.IsNullOrEmpty(tmpModel.Email) ? "" : tmpModel.Email.Trim();
                vModel.SelectedVoucherSum = vModel.Vouchers.Where(v => v.Selected == true).Sum(i => i.Payment);
            }
            if (!string.IsNullOrEmpty(button) && vModel.SelectedVoucherSum > 0)
            {
                int newAccoutId = 0;
                string newPassword = "";

                Account account = dbAccount.Get(vModel.Email);

                if (account == null && !string.IsNullOrEmpty(tmpModel.FirstName) && !string.IsNullOrEmpty(tmpModel.LastName) && (!string.IsNullOrEmpty(tmpModel.Mobile) || !string.IsNullOrEmpty(tmpModel.Phone)))
                {
                    newAccoutId = dbAccount.Insert(tmpModel.FirstName, tmpModel.LastName, tmpModel.Email, new DateTime(1900, 1, 1), "", "", "", "", 0, tmpModel.Phone, tmpModel.Mobile, "", "", "", 0, 0, Parameters.Now, "");
                    newPassword = dbAccount.ResetPassword(tmpModel.Email);
                    account = dbAccount.Get(tmpModel.Email);
                }

                if (account != null)
                {
                    IList<Voucher> SelectedVouchers = vModel.Vouchers.Where(v => v.Selected).ToList<Voucher>();
                    foreach (Voucher voucher in SelectedVouchers)
                    {
                        dbVoucher.Update(eStoreUser.AccountId, voucher.VoucherId, false, true, eStoreUser.AccountId, 0, false, string.IsNullOrEmpty(voucher.Comment) ? tmpModel.Email + " kullanıcısına hediye gönderildi" : voucher.Comment + ", " + tmpModel.Email + " kullanıcısına hediye gönderildi");
                    }
                    long newVoucherId = dbVoucher.Insert(account.AccountId, (short)PaymentTypes.HediyeCeki.GetHashCode(), true, false, dbRefs.GetRef3(2024), new DateTime(Parameters.Now.Year + 1, 1, 1), eStoreUser.AccountId, vModel.SelectedVoucherSum, false, eStoreUser.Email + " kullanıcısından hediye", false, "", 0, 0, 0, Parameters.Now);

                    vModel.ProcessMessage = "Hediye çeki gönderme işleminiz tamamlandı.";
                    vModel.FirstName = "";
                    vModel.LastName = "";
                    vModel.Phone = "";
                    vModel.Mobile = "";


                    vModel.Vouchers = dbVoucher.GetList(eStoreUser.AccountId);
                    if (vModel.Vouchers == null) vModel.Vouchers = new List<Voucher>();
                    foreach (Voucher voucherTemp in tmpModel.Vouchers)
                    {
                        if (!voucherTemp.Selected)
                        {
                            Voucher voucher = vModel.Vouchers.Where(v => v.VoucherId == voucherTemp.VoucherId).FirstOrDefault();
                            if (voucher != null)
                            {
                                voucher.Selected = false;
                            }
                        }
                    }
                    vModel.Email = string.IsNullOrEmpty(tmpModel.Email) ? "" : tmpModel.Email.Trim();
                    vModel.SelectedVoucherSum = vModel.Vouchers.Where(v => v.Selected == true).Sum(i => i.Payment);

                    try
                    {
                        Utils.SendMail(account.Email, "", "", "Hediye Çekiniz Gönderildi", dbRefs.GetRef5(131).Replace("{User}", eStoreUser.Email).Replace("{Link}", "<a href='" + Parameters.WebSiteUrl + "'>" + Parameters.WebSiteUrl + "</a>"), true);
                    }
                    catch (Exception ex)
                    {
                        Logger.Write(ex, "Exception");
                    }
                    if (newAccoutId > 0)
                    {
                        Utils.SendMail(account.Email, "", "", "Hediyenizi kullanabilmeniz için oluşturulan kullanıcı bilgileriniz", dbRefs.GetRef5(130).Replace("{User}", eStoreUser.Email).Replace("{Link}", "<a href='" + Parameters.WebSiteUrl + "'>" + Parameters.WebSiteUrl + "</a>").Replace("{UserName}", account.Email).Replace("{Password}", newPassword), true);
                    }
                }
                else
                {
                    vModel.FirstName = tmpModel.FirstName;
                    vModel.LastName = tmpModel.LastName;
                    vModel.Phone = tmpModel.Phone;
                    vModel.Mobile = tmpModel.Mobile;
                    vModel.DetailInfoNeeded = true;
                    vModel.ProcessMessage = "Hediye çeki göndereceğiniz kullanıcı bulunamadı, istenen bilgileri girmeniz durumunda kullanıcı sistem tarafından açılıp hediye çeki yeni kullanıcı bilgisi ile birlikte gönderilecektir.";
                }

            }
            return PartialView("Get", vModel);
        }
    }
}
